Use case: Input capture in interrupt mode ¶
|
HAL1 |
HAL2 |
|---|---|
|
Start input capture in interrupt mode
TIMx IRQ handler
Callback function
|
Start input capture in interrupt mode
TIMx IRQ handler
Callback function
|
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Declaration of tim handle */
TIM_HandleTypeDef htim3;
/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting the input capture.*/
.............
/* Start the Input Capture in interrupt mode */
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);
/* User must call the Interrupt Handler
functions in TIM3 global interrupt */
void TIM3_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim3);
}
/* Call the callback function */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
/* Get the Input Capture value */
uwIC2Value = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2);
}
|
/* Declaration of the tim handle */
hal_tim_handle_t htim3;
/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting the input capture.*/
.............
/* Start a timer's output channel in interrupt mode. */
HAL_TIM_IC_StartChannel_IT(&htim3, HAL_TIM_CHANNEL_2);
/* Start the timer */
HAL_TIM_Start(&htim3);
/* User must call the Interrupt Handler
functions in TIM3 global interrupt */
void TIM3_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim3);
}
/* Call the callback function */
void HAL_TIM_InputCaptureCallback(hal_tim_handle_t *htim,
hal_tim_channel_t channel);
{
/* Get the Input Capture value */
uwIC2Value = HAL_TIM_IC_ReadChannelCapturedValue(&htim3, HAL_TIM_CHANNEL_2)
}
|
Key changes ¶
Input capture start API changes from
HAL_TIM_IC_Start_IT()toHAL_TIM_IC_StartChannel_IT()and requiresHAL_TIM_Start().The callback is renamed from
HAL_TIM_IC_CaptureCallback()toHAL_TIM_InputCaptureCallback().
![@startuml
skinparam maxMessageSize 440
' To add a number by line
'autonumber
title "Input Capture in HAL 1"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
participant "IT" as IT
group Initialization and configuration of TIMx
App -[hidden]>Driver
end
group Configuration of channel
App -> Driver : \n<b><font color=blue>HAL_TIM_IC_ConfigChannel</b>(channel = <font color=blue>HAL_TIM_CHANNEL_<b>x</b>,\n(TIM_IC_InitTypeDef) {\n.ICPolarity = TIM_ICPOLARITY_RISING,\n.ICSelection = TIM_ICSELECTION_DIRECTTI,\n.ICPrescaler = TIM_ICPSC_DIV1,\n.ICFilter = 0})
Driver --> App : HAL_OK
end
group Start TIMx in IT mode
App -> Driver : HAL_TIM_IC_Start_IT(&htim, HAL_TIM_CHANNEL_<b>x</b>)
Driver --> App : HAL_OK
Driver->Driver : Enable capture compare interrupt\nEnable TIMx counter
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
Driver -> App: HAL_TIM_IC_CaptureCallback()
App --> Driver
Driver --> IT
end
@enduml](../../../../../_images/plantuml-27c4d2aa38ba6a63f999e602692ef56f23568147.png)
![@startuml
skinparam maxMessageSize 440
' To add a number by line
'autonumber
title "Input Capture in HAL 2"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
participant "IT" as IT
group Initialization and configuration of TIMx
App -[hidden]>Driver
end
group Configuration of channel
App -> Driver : \n<b><font color=blue>HAL_TIM_IC_SetConfigChannel</b>(channel = <font color=blue>HAL_TIM_CHANNEL_<b>x</b>,\n(hal_tim_ic_channel_config_t) {\n.polarity = HAL_TIM_IC_RISING,\n.selection = HAL_TIM_IC_DIRECTTI,\n.prescaler = HAL_TIM_IC_DIV1,\n.filter = 0})
Driver --> App : HAL_OK
App -> Driver : \n<b><font color=blue>HAL_TIM_IC_SetConfigCaptureUnit</b>(\ncompare_unit = <font color=blue>HAL_TIM_IC_CAPTURE_UNIT_<b>x</b>,\n(hal_tim_ic_capture_unit_config_t ) {\n.mode = <font color=black>HAL_TIM_IC_PWM1})
Driver --> App : HAL_OK
end
group Start TIMx in IT mode
App -> Driver : HAL_TIM_IC_StartChannel_IT(HAL_TIM_CHANNEL_<b>x</b>)
Driver --> App : HAL_OK
Driver->Driver : Enable the channel\nEnable capture compare interrupt.
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
App -> Driver : HAL_TIM_Start()
Driver->Driver : Enable TIMx counter
Driver --> App : HAL_OK
Driver -> App: HAL_TIM_InputCaptureCallback()
end
@enduml](../../../../../_images/plantuml-f9c40aef6ade598837fd2b3ff3f099ab5107b942.png)